var express = require('express');
var router = express.Router();
const billModel = require('../models/model')
router.get('/', function(req, res, next) {
  billModel.find().then((data) => { 
    res.render('index', { title: '记账本' , data:data});
   })
   .catch(() => { 
      res.status(500).send('查找失败')  
  })
  
});
router.get('/create', function(req, res, next) {
  res.render('create', { title: '添加记账本' });
});

router.post('/add',function(req,res,next){
  billModel.create(req.body).then((data) => { 
    res.render('success',{title:'添加成功',url:'/'})
  }).catch(error=>{
    res.status(500).send('插入失败~')
  })
  
})

router.get('/delete/:id',function(req,res,next){
  // console.log(req.params.id)
  // res.send('nihaoa')
  billModel.deleteOne({_id:req.params.id}).then(() => { 
    res.render('success',{title:'删除成功',url:'/'})
  }).catch(() => { 
    res.status(500).send('删除失败~')
   })
})

module.exports = router;
